<?php
namespace Club\Controller;
use Think\Controller;

class TrainController extends PublicController {
    public function trainlist(){
        $train=M('train');
        $train=$train->select();
        $this->assign("train",$train);
        $this->display();
    }
    //添加活动王子恒
    public function add(){
        $train = M('train');
        if($train->create()){
            if ($_FILES['pic']['error'] == 0) {
                $info = $this->upload();
                $name=$info['pic']['savename'];
            } else {
                $name = "123.jpg";
            }
            $train->pic=$name;
            if (false !== $train->add()) {
                $this->success('添加成功！', U('trainlist'));
            } else {         
                $this->error('数据错误','');           
            }
        }else {            
            $this->error($train->getError());  
        }
    }

        public function upload(){
        $upload = new \Think\Upload();
        $upload->maxSize = 3145728 ;
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath = './Public/photo/';
        $upload->autoSub=false;
        $info = $upload->upload();

        if(!$info) {        
            $this->error($upload->getError());    
        }else{        
           return $info; 
        }                    
    }

     //删除活动：王子恒
    public function delete(){
        $id=$_GET['trainid'];
        $train=M('train');
        $train=$train->where("trainid=$id")->delete();
        if($train=1){
            $this -> success('删除成功！',U('trainlist'));
        }else{
            $this->error('删除失败！','');
        }
    }

    public function edittrain(){
        $id=$_GET['trainid'];
        $train=M('train');
        $train =$train->where("trainid=$id")->find();
        $this->assign('train',$train);
        $this->display();
    }


    /*修改活动 开发人：王子恒*/
     public function edit(){
        $id=$_GET['trainid'];
        $train = M('train');
        $pic=$train->where("trainid=$id")->find();
        if($train->create()){
            if ($_FILES['pic']['error'] == 0) {
                $info = $this->upload();
                $name=$info['pic']['savename'];
                unlink(realpath("./Public/photo/".$pic["pic"]));
            } else {
                $name = $pic["pic"];
            }
            $train->pic=$name;
            if (false !== $train->where("trainid=$id")->save()) {
                $this->success('修改成功！', U('trainlist'));
            } else {         
                $this->error('数据错误','');           
            }
        }else {            
            $this->error($train->getError());  
        }
    }


}